package com.mms.utsc.smartnet;

import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class TimetableActivity extends Activity {
	ArrayList<TextView> views = new ArrayList<TextView>();
	private final String days = "motuwethfr";
	private final int[] colors = { Color.BLUE, Color.CYAN, Color.rgb(255, 165, 0), Color.GREEN, Color.YELLOW, Color.rgb(255, 192, 203), Color.MAGENTA };

	@Override
	protected void onCreate(Bundle savedInstatnceState) {
		super.onCreate(savedInstatnceState);
		setContentView(R.layout.timetable);
		ProgressDialog pd = ProgressDialog.show(this, "Loading...", "Loading - Please wait...");
		DBHandler db = new DBHandler(pd, this);
		LocalDBHandler localDB = db.getLocalDBHandler();
		populateViews();
		ArrayList<Schedule> courses = localDB.getAllSchedules();
		db.close();
		generateTable(courses);
		pd.dismiss();

	}

	@SuppressWarnings("deprecation")
	void generateTable(ArrayList<Schedule> courses) {
		for (int i = 0; i < courses.size(); i++) {
			int start = courses.get(i).getStartTime().getHours();
			int end = courses.get(i).getEndTime().getHours();
			int day = days.indexOf(courses.get(i).getDay().substring(0, 2).toLowerCase());
			if ((end - start) == 1) {
				views.get((start - 7) * 5 + (day / 2)).setText(courses.get(i).getCourse());
				views.get((start - 7) * 5 + (day / 2)).setBackgroundColor(colors[i]);
			} else {
				for (int j = start; j < end; j++) {
					if (j == start)
						views.get((start - 7) * 5 + (day / 2)).setText(courses.get(i).getCourse());
					views.get((start - 7) * 5 + (day / 2)).setBackgroundColor(colors[i]);
				}
			}
		}
	}

	void populateViews() {
		views.add((TextView) findViewById(R.id.textView12));
		views.add((TextView) findViewById(R.id.textView13));
		views.add((TextView) findViewById(R.id.textView14));
		views.add((TextView) findViewById(R.id.textView15));
		views.add((TextView) findViewById(R.id.textView16));
		views.add((TextView) findViewById(R.id.textView22));
		views.add((TextView) findViewById(R.id.textView23));
		views.add((TextView) findViewById(R.id.textView24));
		views.add((TextView) findViewById(R.id.textView25));
		views.add((TextView) findViewById(R.id.textView26));
		views.add((TextView) findViewById(R.id.textView32));
		views.add((TextView) findViewById(R.id.textView33));
		views.add((TextView) findViewById(R.id.textView34));
		views.add((TextView) findViewById(R.id.textView35));
		views.add((TextView) findViewById(R.id.textView36));
		views.add((TextView) findViewById(R.id.textView42));
		views.add((TextView) findViewById(R.id.textView43));
		views.add((TextView) findViewById(R.id.textView44));
		views.add((TextView) findViewById(R.id.textView45));
		views.add((TextView) findViewById(R.id.textView46));
		views.add((TextView) findViewById(R.id.textView52));
		views.add((TextView) findViewById(R.id.textView53));
		views.add((TextView) findViewById(R.id.textView54));
		views.add((TextView) findViewById(R.id.textView55));
		views.add((TextView) findViewById(R.id.textView56));
		views.add((TextView) findViewById(R.id.textView62));
		views.add((TextView) findViewById(R.id.textView63));
		views.add((TextView) findViewById(R.id.textView64));
		views.add((TextView) findViewById(R.id.textView65));
		views.add((TextView) findViewById(R.id.textView66));
		views.add((TextView) findViewById(R.id.textView72));
		views.add((TextView) findViewById(R.id.textView73));
		views.add((TextView) findViewById(R.id.textView74));
		views.add((TextView) findViewById(R.id.textView75));
		views.add((TextView) findViewById(R.id.textView76));
		views.add((TextView) findViewById(R.id.textView82));
		views.add((TextView) findViewById(R.id.textView83));
		views.add((TextView) findViewById(R.id.textView84));
		views.add((TextView) findViewById(R.id.textView85));
		views.add((TextView) findViewById(R.id.textView86));
		views.add((TextView) findViewById(R.id.textView92));
		views.add((TextView) findViewById(R.id.textView93));
		views.add((TextView) findViewById(R.id.textView94));
		views.add((TextView) findViewById(R.id.textView95));
		views.add((TextView) findViewById(R.id.textView96));
		views.add((TextView) findViewById(R.id.textView102));
		views.add((TextView) findViewById(R.id.textView103));
		views.add((TextView) findViewById(R.id.textView104));
		views.add((TextView) findViewById(R.id.textView105));
		views.add((TextView) findViewById(R.id.textView106));
		views.add((TextView) findViewById(R.id.textView112));
		views.add((TextView) findViewById(R.id.textView113));
		views.add((TextView) findViewById(R.id.textView114));
		views.add((TextView) findViewById(R.id.textView115));
		views.add((TextView) findViewById(R.id.textView116));
		views.add((TextView) findViewById(R.id.textView122));
		views.add((TextView) findViewById(R.id.textView123));
		views.add((TextView) findViewById(R.id.textView124));
		views.add((TextView) findViewById(R.id.textView125));
		views.add((TextView) findViewById(R.id.textView126));
		views.add((TextView) findViewById(R.id.textView132));
		views.add((TextView) findViewById(R.id.textView133));
		views.add((TextView) findViewById(R.id.textView134));
		views.add((TextView) findViewById(R.id.textView135));
		views.add((TextView) findViewById(R.id.textView136));
		views.add((TextView) findViewById(R.id.textView142));
		views.add((TextView) findViewById(R.id.textView143));
		views.add((TextView) findViewById(R.id.textView144));
		views.add((TextView) findViewById(R.id.textView145));
		views.add((TextView) findViewById(R.id.textView146));
		views.add((TextView) findViewById(R.id.textView152));
		views.add((TextView) findViewById(R.id.textView153));
		views.add((TextView) findViewById(R.id.textView154));
		views.add((TextView) findViewById(R.id.textView155));
		views.add((TextView) findViewById(R.id.textView156));
		views.add((TextView) findViewById(R.id.textView162));
		views.add((TextView) findViewById(R.id.textView163));
		views.add((TextView) findViewById(R.id.textView164));
		views.add((TextView) findViewById(R.id.textView165));
		views.add((TextView) findViewById(R.id.textView166));

	}
}
